<%-- 
    Document   : policy
    Created on : 2013-5-7, 12:50:46
    Author     : piaohao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>政策展现</title>
        <script>
            Ext.onReady(function() {
                var menu = Ext.create('Ext.menu.Menu', {
                    items: [{
                            id: "viewItem",
                            text: '查看',
                            icon: "resource/images/ico/search.png",
                            handler: function() {
                                var id = policyTree.getSelectionModel().getSelection()[0].raw.id;
                                var detailWin = Ext.create("Ext.window.Window", {
                                    title: "文档信息",
                                    maximized: true,
                                    maximizable: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    html: "<iframe id='detailFrame' src='policy/view?id=" + id + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWin.close();
                                            }}
                                    ]
                                });
                                detailWin.show();
                            }}, "-", {
                            id: "addItem",
                            text: "添加政策",
                            icon: "resource/images/ico/add.png",
                            handler: function() {
                                var id = policyTree.getSelectionModel().getSelection()[0].raw.id;
                                var p_id = policyTree.getSelectionModel().getSelection()[0].raw.p_id;
                                var path = policyTree.getSelectionModel().getSelection()[0].raw.path;
                                var leaf = policyTree.getSelectionModel().getSelection()[0].raw.leaf;
                                var form = Ext.create("Ext.form.Panel", {
                                    url: "policy/add",
                                    items: [
                                        {xtype: "hidden", name: "policy.id", value: id},
                                        {xtype: "hidden", name: "policy.p_id", value: p_id},
                                        {xtype: "hidden", name: "policy.path", value: path},
                                        {xtype: "hidden", name: "policy.leaf", value: leaf},
                                        {xtype: "filefield", fieldLabel: "政策文件", id: "addFile", name: "file", allowBlank: false, buttonText: "选择文件", width: 400},
                                        {xtype: "displayfield", fieldLabel: "备注", value: "此文件必须为doc|docx|pdf格式"}
                                    ],
                                    buttons: [{
                                            text: '重置',
                                            iconCls: "refresh",
                                            handler: function() {
                                                this.up('form').getForm().reset();
                                            }
                                        }, {
                                            text: '提交',
                                            formBind: true, //only enabled once the form is valid
                                            disabled: true,
                                            iconCls: "save",
                                            handler: function() {
                                                var path = Ext.getCmp("addFile").getValue();
                                                if (path.indexOf(".doc") !== -1 || path.indexOf(".docx") !== -1 || path.indexOf(".pdf") !== -1) {

                                                } else {
                                                    Ext.Msg.alert("提示", "此文件必须为doc|docx|pdf格式！");
                                                    return;
                                                }
                                                var mask = new Ext.LoadMask(policyTree, {msg: "正在处理..."});
                                                mask.show();
                                                var form = this.up('form').getForm();
                                                if (form.isValid()) {
                                                    form.submit({
                                                        success: function(form, action) {
                                                            var json = action.result;
                                                            Ext.Msg.alert('提示', json.msg, function() {
                                                                var p = json.policy;
                                                                var id = policyTree.getSelectionModel().getSelection()[0].get("id");
                                                                var node = policyTree.getStore().getNodeById(id);
                                                                node.appendChild({
                                                                    id: p.id,
                                                                    icon: p.icon,
                                                                    text: p.text,
                                                                    path: p.path,
                                                                    leaf: p.leaf,
                                                                    p_id: p.p_id,
                                                                    swf_path: p.swf_path,
                                                                    upload_time: p.upload_time,
                                                                    org_id: p.org_id
                                                                });
                                                                win.close();
                                                                mask.hide();
                                                            });
                                                        },
                                                        failure: function(form, action) {
                                                            Ext.Msg.alert('提示', action.result.msg);
                                                            mask.hide();
                                                        }
                                                    });
                                                }
                                            }
                                        }, {
                                            text: "关闭",
                                            iconCls: "close",
                                            handler: function() {
                                                win.close();
                                            }
                                        }]
                                });
                                var win = Ext.create("Ext.window.Window", {
                                    title: "政策法规",
                                    width: 550,
                                    height: 300,
                                    maximizable: true,
                                    //closeAction: "hide",
                                    layout: "fit",
                                    items: [form]
                                });
                                win.show();
                            }
                        }, "-", {
                            id: "deleteItem",
                            text: "删除",
                            icon: "resource/images/ico/remove.png",
                            handler: function() {
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(policyTree, {msg: "正在删除..."});
                                        mask.show();
                                        var sel = policyTree.getSelectionModel().getSelection();
                                        var ids = [];
                                        for (i = 0; i < sel.length; i++) {
                                            ids.push(sel[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: "policy/delete",
                                            params: {
                                                ids: ids
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    var id = policyTree.getSelectionModel().getSelection()[0].get("id");
                                                    policyTree.getStore().getNodeById(id).remove();
                                                });
                                                mask.hide();
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {

                                                });
                                                mask.hide();
                                            }
                                        });
                                    }
                                });
                            }
                        }]
                });
                var policyStore = Ext.create("Ext.data.TreeStore", {
                    proxy: {
                        type: 'ajax',
                        url: 'policy/getTree'
                    },
                    nodeParam: "id",
                    fields: [
                        {name: 'text', type: 'string'},
                        {name: 'path', type: 'string'},
                        {name: 'id', type: 'string'},
                        {name: 'leaf', type: 'boolean'}

                    ],
                    root: {
                        text: '根节点',
                        id: 'root',
                        expanded: true
                    }
                });
                var policyTree = Ext.create("Ext.tree.TreePanel", {
                    store: policyStore,
                    rootVisible: false,
                    listeners: {
                        itemcontextmenu: function(view, record, item, index, event) {
                            event.preventDefault();
                            if (!record.raw.leaf) {
                                Ext.getCmp("viewItem").setDisabled(true);
                                Ext.getCmp("addItem").setDisabled(false);
                                Ext.getCmp("deleteItem").setDisabled(true);
                            } else {
                                Ext.getCmp("viewItem").setDisabled(false);
                                Ext.getCmp("addItem").setDisabled(true);
                                Ext.getCmp("deleteItem").setDisabled(false);
                            }
                            menu.showAt(event.getXY());
                        },
                        render: function() {
                            policyTree.expandAll();
                        }
                    },
                    tbar: [
                        {
                            text: "初始化政策文件",
                            iconCls: "rule",
                            handler: function() {
                                var form = Ext.create("Ext.form.Panel", {
                                    url: "policy/init",
                                    items: [
                                        {xtype: "filefield", fieldLabel: "政策文件", id: "initFile", name: "file", allowBlank: false, buttonText: "选择文件", width: 400},
                                        {xtype: "displayfield", fieldLabel: "备注", value: "此文件必须为zip|rar|7z格式"}
                                    ],
                                    buttons: [{
                                            text: '重置',
                                            iconCls: "refresh",
                                            handler: function() {
                                                this.up('form').getForm().reset();
                                            }
                                        }, {
                                            text: '提交',
                                            formBind: true, //only enabled once the form is valid
                                            disabled: true,
                                            iconCls: "save",
                                            handler: function() {
                                                var path = Ext.getCmp("initFile").getValue();
                                                if (path.indexOf(".rar") !== -1 || path.indexOf(".zip") !== -1 || path.indexOf(".7z") !== -1) {

                                                } else {
                                                    Ext.Msg.alert("提示", "此文件必须为zip|rar|7z格式！");
                                                    return;
                                                }
                                                var mask = new Ext.LoadMask(policyTree, {msg: "正在处理..."});
                                                mask.show();
                                                var form = this.up('form').getForm();
                                                if (form.isValid()) {
                                                    form.submit({
                                                        success: function(form, action) {
                                                            var json = action.result;
                                                            Ext.Msg.alert('提示', json.msg, function() {
                                                                win.close();
                                                                mask.hide();
                                                            });
                                                        },
                                                        failure: function(form, action) {
                                                            Ext.Msg.alert('提示', action.result.msg);
                                                            mask.hide();
                                                        }
                                                    });
                                                }
                                            }
                                        }, {
                                            text: "关闭",
                                            iconCls: "close",
                                            handler: function() {
                                                win.close();
                                            }
                                        }]
                                });
                                var win = Ext.create("Ext.window.Window", {
                                    title: "政策法规",
                                    width: 550,
                                    height: 300,
                                    maximizable: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    items: [form]
                                });
                                win.show();
                            }
                        }, "-", {
                            text: "展开全部",
                            iconCls: "add",
                            handler: function() {
                                policyTree.expandAll();
                            }
                        }, "-", {
                            text: "收起全部",
                            iconCls: "remove",
                            handler: function() {
                                policyTree.collapseAll();
                            }
                        }, "-", {
                            id: "policyName",
                            fieldLabel: "文件名",
                            xtype: "textfield",
                            enableKeyEvents:true,
                            listeners: {
                                keyup: function(field, e) {
                                    if (e.getKey() === Ext.EventObject.ENTER) {
                                        var name = Ext.getCmp("policyName").getValue();
                                        if (name === "") {
                                            return;
                                        }
                                        var nodeArr = [];
                                        function findchildnode(node) {
                                            var childnodes = node.childNodes;
                                            for (var i = 0; i < childnodes.length; i++) {//从节点中取出子节点依次遍历
                                                var rootnode = childnodes[i];
                                                if (rootnode.childNodes.length > 0) {//判断子节点下是否存在子节点，
                                                    findchildnode(rootnode);//如果存在子节点  递归
                                                } else {
                                                    nodeArr.push(rootnode);
                                                }
                                            }
                                        }
                                        findchildnode(policyStore.getRootNode());
                                        var pStore = Ext.create('Ext.data.Store', {
                                            fields: ["id", "text", "swf_path"],
                                            data: {
                                                data: []
                                            },
                                            proxy: {
                                                type: 'memory',
                                                reader: {
                                                    type: 'json',
                                                    root: 'data'
                                                }
                                            },
                                            autoLoad: true
                                        });
                                        var pGrid = Ext.create("Ext.grid.Panel", {
                                            columnLines: true,
                                            frame: true,
                                            selType: 'checkboxmodel', // 设置选择模式 
                                            multiSelect: true, // 允许多选
                                            forceFit: true, columns: [
                                                {header: '主键', dataIndex: 'id', hidden: true},
                                                {header: '政策名称', dataIndex: 'text', width: 120},
                                                {header: 'swf路径', dataIndex: 'swf_path', hidden: true}
                                            ],
                                            store: pStore,
                                            tbar: [
                                                {
                                                    text: "查看",
                                                    iconCls: "search",
                                                    handler: function() {
                                                        var sel = pGrid.getSelectionModel().getSelection();
                                                        if (sel.length !== 1) {
                                                            Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                                            return;
                                                        }
                                                        var detailWin = Ext.create("Ext.window.Window", {
                                                            title: "文档信息",
                                                            maximized: true,
                                                            maximizable: true,
                                                            closeAction: "hide",
                                                            layout: "fit",
                                                            html: "<iframe id='detailFrame' src='policy/view?id=" + sel[0].raw.id + "' width='100%' height='100%'></iframe>",
                                                            buttons: [
                                                                {text: "打印", iconCls: "print",
                                                                    handler: function() {
                                                                        Ext.get("detailFrame").dom.contentWindow.print();
                                                                    }},
                                                                {text: "关闭", iconCls: "close",
                                                                    handler: function() {
                                                                        detailWin.close();
                                                                    }}
                                                            ]
                                                        });
                                                        detailWin.show();
                                                    }
                                                }
                                            ]
                                        });
                                        for (i = 0; i < nodeArr.length; i++) {
                                            if (nodeArr[i].raw.text.indexOf(name) !== -1) {
                                                pStore.add({
                                                    id: nodeArr[i].raw.id,
                                                    text: nodeArr[i].raw.text,
                                                    swf_path: nodeArr[i].raw.swf_path
                                                });
                                            }
                                        }
                                        var pWin = Ext.create("Ext.window.Window", {
                                            title: "搜索结果",
                                            //maximized: true,
                                            modal: true,
                                            width: 800,
                                            height: 400,
                                            maximizable: true,
                                            closeAction: "hide",
                                            layout: "fit",
                                            items: [pGrid]
                                        });
                                        pWin.show();
                                    }
                                }
                            }
                        }, {
                            text: "搜索",
                            handler: function() {
                                var name = Ext.getCmp("policyName").getValue();
                                if (name === "") {
                                    return;
                                }
                                var nodeArr = [];
                                function findchildnode(node) {
                                    var childnodes = node.childNodes;
                                    for (var i = 0; i < childnodes.length; i++) {//从节点中取出子节点依次遍历
                                        var rootnode = childnodes[i];
                                        if (rootnode.childNodes.length > 0) {//判断子节点下是否存在子节点，
                                            findchildnode(rootnode);//如果存在子节点  递归
                                        } else {
                                            nodeArr.push(rootnode);
                                        }
                                    }
                                }
                                findchildnode(policyStore.getRootNode());
                                var pStore = Ext.create('Ext.data.Store', {
                                    fields: ["id", "text", "swf_path"],
                                    data: {
                                        data: []
                                    },
                                    proxy: {
                                        type: 'memory',
                                        reader: {
                                            type: 'json',
                                            root: 'data'
                                        }
                                    },
                                    autoLoad: true
                                });
                                var pGrid = Ext.create("Ext.grid.Panel", {
                                    columnLines: true,
                                    frame: true,
                                    selType: 'checkboxmodel', // 设置选择模式 
                                    multiSelect: true, // 允许多选
                                    forceFit: true, columns: [
                                        {header: '主键', dataIndex: 'id', hidden: true},
                                        {header: '政策名称', dataIndex: 'text', width: 120},
                                        {header: 'swf路径', dataIndex: 'swf_path', hidden: true}
                                    ],
                                    store: pStore,
                                    tbar: [
                                        {
                                            text: "查看",
                                            iconCls: "search",
                                            handler: function() {
                                                var sel = pGrid.getSelectionModel().getSelection();
                                                if (sel.length !== 1) {
                                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                                    return;
                                                }
                                                var detailWin = Ext.create("Ext.window.Window", {
                                                    title: "文档信息",
                                                    maximized: true,
                                                    maximizable: true,
                                                    closeAction: "hide",
                                                    layout: "fit",
                                                    html: "<iframe id='detailFrame' src='policy/view?id=" + sel[0].raw.id + "' width='100%' height='100%'></iframe>",
                                                    buttons: [
                                                        {text: "打印", iconCls: "print",
                                                            handler: function() {
                                                                Ext.get("detailFrame").dom.contentWindow.print();
                                                            }},
                                                        {text: "关闭", iconCls: "close",
                                                            handler: function() {
                                                                detailWin.close();
                                                            }}
                                                    ]
                                                });
                                                detailWin.show();
                                            }
                                        }
                                    ]
                                });
                                for (i = 0; i < nodeArr.length; i++) {
                                    if (nodeArr[i].raw.text.indexOf(name) !== -1) {
                                        pStore.add({
                                            id: nodeArr[i].raw.id,
                                            text: nodeArr[i].raw.text,
                                            swf_path: nodeArr[i].raw.swf_path
                                        });
                                    }
                                }
                                var pWin = Ext.create("Ext.window.Window", {
                                    title: "搜索结果",
                                    //maximized: true,
                                    modal: true,
                                    width: 800,
                                    height: 400,
                                    maximizable: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    items: [pGrid]
                                });
                                pWin.show();
                            }
                        }
                    ]
                });
                Ext.create("Ext.Viewport", {
                    layout: "border",
                    items: [
                        {region: "center", layout: "fit", items: [policyTree]}
                    ]
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>
